<?php
defined( '_VALID_MOS' ) or die('<script>window.location.href="http://www.kinifurniture.com";</script>');

CLASS DB_MYSQL {

	var $Host;				// Hostname of our MySQL server.
	var $Database;			// Logical database name on that server.
	var $User;				// User and Password for login.
	var $Password;
	var $Link_ID  = 0;		// Result of mysql_connect().
	var $Query_ID = 0;		// Result of most recent mySQL_query().
	var $record = array();	// Current mysql_fetch_array()-result.
	var $Row;				// Current row number.
	var $Errno = 0;			// Error state of query...
	var $Error = "";
	var $DDtable="_dt";		// Data Dictionary Tables
	var $DDfield="_df";		// Data Dictionary Fields
	
	function check_logon() {
		$all_okay=false;
		$error_msg="All is okay";
		if ( 0 == $this->Link_ID ) { $this->Link_ID=mysql_pconnect($this->Host, $this->User, $this->Password); }

		if (!$this->Link_ID) {
			$error_msg = "Link-ID == false, connect failed";
		} else if (!mySQL_query(sprintf("use %s",$this->Database),$this->Link_ID)) {
			$error_msg = "cannot use database ".$this->Database;
		} else if (!mySQL_query(sprintf("select TableName from %s",$this->DDtable),$this->Link_ID)) {
			$error_msg = "no datadictionary tables in ".$this->Database;
		} else if (!mySQL_query(sprintf("select TableName from %s",$this->DDfield),$this->Link_ID)) {
			$error_msg = "no datadictionary fields in ".$this->Database;
		} else {
			$all_okay=true;
		}
		
		if (!$all_okay) {
			$this->Errno = mysql_errno();
			$this->Error = mysql_error();
			$this->warn_not_loggedon($error_msg);
		}
		return $all_okay;
	}

	function warn_not_loggedon($msg) {
		printf("</td></tr></TABLE><b>Not Logged on to Database:</b><br> %s<br>\n", $msg);
		printf("<b>MySQL Error</b>: %s (%s)<p>\n",
			$this->Errno,
			$this->Error);
		printf("Host     : %s<br>\n",$this->Host);
		printf("Database : %s<br>\n",$this->Database);
		printf("User     : %s<br>\n",$this->User);
	}

	function halt($msg) {
//	if($_SESSION['SESSIONUSERID']=="0")
//		{
		printf("</td></tr></TABLE><b>Database error:</b> %s<br>\n", $msg);
		printf("<b>MySQL Error</b>: %s (%s)<br>\n",
			$this->Errno,
			$this->Error);
		echo "<p>\n";
		echo "Host :	".$this->Host."<BR>\n";
		echo "Database :	".$this->Database."<BR>\n";
		echo "User :	".$this->User."<BR>\n";
		die("Session halted.");
//		} else
//		echo "Mysql Error !!!";

	}

	function warn($msg) {
		printf("</td></tr></TABLE><b>Database warning:</b> %s<br>\n", $msg);
		printf("<b>MySQL Error</b>: %s (%s)<p>\n",
			$this->Errno,
			$this->Error);
	}

	function connect() {
		if ( 0 == $this->Link_ID ) {

			$this->Link_ID=mysql_pconnect($this->Host, $this->User, $this->Password);
			if (!$this->Link_ID) {
				$this->halt("Link-ID == false, connect failed".mysql_error());
			}
			if (!mySQL_query(sprintf("use %s",$this->Database),$this->Link_ID)) {
				$this->halt("cannot use database ".$this->Database);
			}
		}
	}


	function query($Query_String) {
		$this->connect();
		$this->Query_ID = mySQL_query($Query_String,$this->Link_ID);
		$this->Row   = 0;
		$this->Errno = mysql_errno();
		$this->Error = mysql_error();
		if (!$this->Query_ID) {
			$this->halt("Invalid SQL: ".$Query_String);
		}

	  return $this->Query_ID;
	}

 	function no_stop_query($Query_String) {
		$this->connect();
		$this->Query_ID = mySQL_query($Query_String,$this->Link_ID);
		$this->Row   = 0;
		$this->Errno = mysql_errno();
		$this->Error = mysql_error();
		if (!$this->Query_ID) {
		}

	  return $this->Query_ID;
	}

	function table_exist($table) {
		$this->connect();
	
		$Query_String = "select * from ".$table." limit 1";
		$this->Query_ID = mySQL_query($Query_String,$this->Link_ID);
		$this->Row   = 0;
		$this->Errno = mysql_errno();
		$this->Error = mysql_error();
		if(!$this->Query_ID)	return 0;
		else					return 1;
	} 

	function next_record() {
		$this->record = mysql_fetch_array($this->Query_ID,MYSQL_ASSOC);
		$this->Row   += 1;
		$this->Errno = mysql_errno();
		$this->Error = mysql_error();
	
		$stat = is_array($this->record);
		if (!$stat) {
			mysql_free_result($this->Query_ID);
			$this->Query_ID = 0;
		}
		return $stat;
	}

	function next_raw_record() {
		$this->record = mysql_fetch_array($this->Query_ID,MYSQL_ASSOC);
		$this->Row   += 1;
		$this->Errno = mysql_errno();
		$this->Error = mysql_error();
	
		$stat = is_array($this->record);
		if (!$stat) {
			mysql_free_result($this->Query_ID);
			$this->Query_ID = 0;
		}

		return $this->record;
	}

	function seek($pos) {
		$status = mysql_data_seek($this->Query_ID, $pos);
		if ($status)
			$this->Row = $pos;
		return;
	}

	function num_rows() {
		return mysql_num_rows($this->Query_ID);
	}

	function num_fields() {
		return mysql_num_fields($this->Query_ID);
	}

	function f($Name) {
		return $this->record[$Name];
	}

	function p($Name) {
		print $this->record[$Name];
	}

	function affected_rows() {
		return mysql_affected_rows($this->Link_ID);
	}

	function auto_ID() {
		return mysql_insert_id($this->Link_ID);
	}

	function fetch_row() {
		$this->record = mysql_fetch_row($this->Query_ID,MYSQL_ASSOC);
		$this->Row   += 1;
		$this->Errno = mysql_errno();
		$this->Error = mysql_error();
	
		$stat = is_array($this->record);
		if (!$stat) {
			mysql_free_result($this->Query_ID);
			$this->Query_ID = 0;
		}

		return $this->record;
	}

	function field_link($table) {
		return mysql_list_fields($this->Database,$table,$this->Link_ID);
	}

	function field_name($field_num) {
		return mysql_field_name($this->Query_ID,$field_num);
	}

	function field_names() {
		for ($i=0; $i<=$this->num_fields()-1; $i++) {
			 $fields[$i]=$this->field_name($i);
		} 
		return $fields;
	}

	function field_len($table,$field_num) {
		return mysql_field_len($this->Query_ID,$field_num);
	}

	function field_type($table,$field_num) {
		return mysql_field_type($this->Query_ID,$field_num);
	}

	function field_flags($table,$field_num) {
		return mysql_field_flags($this->Query_ID,$field_num);
	}

	function fetch_table($field_num) {
		$obField=$this->fetch_field($field_num);
		return $obField->table;
	} 
	
	function Dbinfomation() {
		$Fieldval = array("Host" => $this->Host,"User" => $this->User, "Password" => $this->Password, "Database" => $this->Database);
		return $Fieldval;
	} 
	
	function fetch_field($field_num) {

		/******************************************************
		The properties of the object are: 
		
		name
		table
		max_length
		not_null
		primary_key
		unique_key
		multiple_key
		numeric
		blob
		type
		unsigned
		zerofill
		/******************************************************
		name:				column name 
		table:			name of the table the column belongs to 
		max_length:		maximum length of the column 
		not_null:		1 if the column cannot be null 
		primary_key:	1 if the column is a primary key 
		unique_key:		1 if the column is a unique key 
		multiple_key:	1 if the column is a non-unique key 
		numeric:			1 if the column is numeric 
		blob:				1 if the column is a BLOB 
		type:				the type of the column 
		unsigned:		1 if the column is unsigned 
		zerofill:		1 if the column is zero-filled 
		/******************************************************/
 		return mysql_fetch_field($this->Query_ID,$field_num);
	} 
}

?>